import { useEffect, useState } from 'react'
import axios from 'axios';
import { useRequest } from 'ahooks';
import { message } from 'antd';
export default function usePublish(publishState: number) {
    const currentUser = JSON.parse(localStorage.getItem('token') as string);
    // 发起网络请求，发布管理-已发布页面，要显示当前用户+发布状态（publishState===2）为 已发布 所有新闻信息,
    const getTableDataSourceRequest = (publishState: number) => {
        return axios.get(`http://localhost:3000/news?_expand=category&author=${currentUser[0].username}&publishState=${publishState}`);
    };
    // 表格数据源保存
    const [dataSource, setDataSource] = useState<any>([]);
    const { run: getTableDataSource, loading: getTableDataSourceLoading } = useRequest(getTableDataSourceRequest, {
        throttleWait: 300,
        manual: true,
        onSuccess: (result: any, params) => {
            if (result.status === 200) {
                console.log('我在执行');
                setDataSource(result.data)
            }
        },
        onError(e, params) {
            message.warning('请求新闻数据失败');
        },
    });
    useEffect(() => {
        getTableDataSource(publishState)
    }, [publishState])
    return (
        {
            dataSource
        }

    )
}
